export interface ServiceConfig {
  env: {
    isProduction: boolean;
    isDevelopment: boolean;
    isTest: boolean;
  };
  redis: {
    host: string;
    port: number;
    password?: string;
    db: number;
  };
  performance: {
    metrics: {
      collectInterval: number;
      healthCheckInterval: number;
      retention: number;
      bufferSize: number;
      defaultTags: Record<string, string>;
    };
    cache: {
      defaultTTL: number;
      maxSize: number;
      checkInterval: number;
    };
  };
  question: {
    difficulty: {
      baseLevel: number;
      adjustmentFactor: number;
      minLevel: number;
      maxLevel: number;
      historyWeight: number;
    };
    batch: {
      importSize: number;
      exportLimit: number;
    };
  };
  scoring: {
    model: string;
    weights: {
      accuracy: number;
      completeness: number;
      relevance: number;
      clarity: number;
    };
    thresholds: {
      minimum: number;
      passing: number;
      excellent: number;
    };
    cache: {
      ttl: number;
      prefix: string;
    };
  };
  analytics: {
    preload: {
      threshold: number;
      interval: number;
    };
    aggregation: {
      timeRanges: string[];
      dimensions: string[];
      metrics: string[];
    };
    cache: {
      ttl: number;
      maxSize: number;
      updateFrequency: number;
      prefix: string;
    };
  };
  monitor: {
    errorLog: {
      maxSize: number;
      retentionDays: number;
    };
    alerts: {
      errorThreshold: number;
      latencyThreshold: number;
      checkInterval: number;
    };
  };
}

const config: ServiceConfig = {
  env: {
    isProduction: process.env.NODE_ENV === 'production',
    isDevelopment: process.env.NODE_ENV === 'development',
    isTest: process.env.NODE_ENV === 'test'
  },
  redis: {
    host: process.env.REDIS_HOST || 'localhost',
    port: parseInt(process.env.REDIS_PORT || '6379', 10),
    password: process.env.REDIS_PASSWORD,
    db: parseInt(process.env.REDIS_DB || '0', 10)
  },
  performance: {
    metrics: {
      collectInterval: parseInt(process.env.METRICS_COLLECT_INTERVAL || '60000', 10),
      healthCheckInterval: parseInt(process.env.HEALTH_CHECK_INTERVAL || '30000', 10),
      retention: parseInt(process.env.METRICS_RETENTION || '86400', 10),
      bufferSize: parseInt(process.env.METRICS_BUFFER_SIZE || '1000', 10),
      defaultTags: {
        service: 'question-service',
        version: process.env.npm_package_version || '1.0.0'
      }
    },
    cache: {
      defaultTTL: parseInt(process.env.CACHE_DEFAULT_TTL || '3600', 10),
      maxSize: parseInt(process.env.CACHE_MAX_SIZE || '10000', 10),
      checkInterval: parseInt(process.env.CACHE_CHECK_INTERVAL || '300000', 10)
    }
  },
  question: {
    difficulty: {
      baseLevel: parseInt(process.env.QUESTION_BASE_LEVEL || '5', 10),
      adjustmentFactor: parseFloat(process.env.QUESTION_ADJUSTMENT_FACTOR || '0.1'),
      minLevel: parseInt(process.env.QUESTION_MIN_LEVEL || '1', 10),
      maxLevel: parseInt(process.env.QUESTION_MAX_LEVEL || '10', 10),
      historyWeight: parseFloat(process.env.QUESTION_HISTORY_WEIGHT || '0.7')
    },
    batch: {
      importSize: parseInt(process.env.QUESTION_IMPORT_BATCH_SIZE || '100', 10),
      exportLimit: parseInt(process.env.QUESTION_EXPORT_LIMIT || '1000', 10)
    }
  },
  scoring: {
    model: process.env.SCORING_MODEL || 'default',
    weights: {
      accuracy: parseFloat(process.env.SCORING_WEIGHT_ACCURACY || '0.4'),
      completeness: parseFloat(process.env.SCORING_WEIGHT_COMPLETENESS || '0.3'),
      relevance: parseFloat(process.env.SCORING_WEIGHT_RELEVANCE || '0.2'),
      clarity: parseFloat(process.env.SCORING_WEIGHT_CLARITY || '0.1')
    },
    thresholds: {
      minimum: parseInt(process.env.SCORING_THRESHOLD_MIN || '60', 10),
      passing: parseInt(process.env.SCORING_THRESHOLD_PASS || '70', 10),
      excellent: parseInt(process.env.SCORING_THRESHOLD_EXCELLENT || '90', 10)
    },
    cache: {
      ttl: parseInt(process.env.SCORING_CACHE_TTL || '3600', 10),
      prefix: process.env.SCORING_CACHE_PREFIX || 'score:'
    }
  },
  analytics: {
    preload: {
      threshold: parseInt(process.env.ANALYTICS_PRELOAD_THRESHOLD || '1000', 10),
      interval: parseInt(process.env.ANALYTICS_PRELOAD_INTERVAL || '300000', 10)
    },
    aggregation: {
      timeRanges: (process.env.ANALYTICS_TIME_RANGES || 'daily,weekly,monthly').split(','),
      dimensions: (process.env.ANALYTICS_DIMENSIONS || 'user,question,category').split(','),
      metrics: (process.env.ANALYTICS_METRICS || 'score,time,attempts').split(',')
    },
    cache: {
      ttl: parseInt(process.env.ANALYTICS_CACHE_TTL || '3600', 10),
      maxSize: parseInt(process.env.ANALYTICS_CACHE_MAX_SIZE || '10000', 10),
      updateFrequency: parseInt(process.env.ANALYTICS_CACHE_UPDATE_FREQUENCY || '300', 10),
      prefix: process.env.ANALYTICS_CACHE_PREFIX || 'analytics:'
    }
  },
  monitor: {
    errorLog: {
      maxSize: parseInt(process.env.MONITOR_ERROR_LOG_MAX_SIZE || '1000', 10),
      retentionDays: parseInt(process.env.MONITOR_ERROR_LOG_RETENTION_DAYS || '30', 10)
    },
    alerts: {
      errorThreshold: parseInt(process.env.MONITOR_ERROR_THRESHOLD || '50', 10),
      latencyThreshold: parseInt(process.env.MONITOR_LATENCY_THRESHOLD || '1000', 10),
      checkInterval: parseInt(process.env.MONITOR_CHECK_INTERVAL || '60000', 10)
    }
  }
};

export { config };
